package com.xinnet.mms.web;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.dubbo.common.logger.Logger;
import com.alibaba.dubbo.common.logger.LoggerFactory;
import com.xinnet.core.utils.page.PageView;
import com.xinnet.mms.allot.service.CustomAllotConfigCheckService;
import com.xinnet.mms.config.service.FlowTurnConfigService;
import com.xinnet.mms.dept.service.DeptService;
import com.xinnet.mms.entity.CustomAllotConfigDept;
import com.xinnet.mms.entity.Dept;
import com.xinnet.mms.entity.Emp;
import com.xinnet.mms.utils.AgainPageView;
/**
 * 
 * 
 * @author 王湛智[wangzhanzhi@xinnet.com]
 *
 * @version 1.0
 *
 * @since 2014年2月14日
 *
 */
@Controller
@RequestMapping("/check")
public class ConfigCheckController {
	@Autowired
	private CustomAllotConfigCheckService allotConfigCheckService;
	@Autowired
	private FlowTurnConfigService flowTurnConfigService;
	@Autowired
	private DeptService deptService;
	
	static Logger log=LoggerFactory.getLogger(ConfigCheckController.class);
	/**
	 * 
	 * 功能描述：显示审核状态的客户分配设置列表
	 *
	 * @param request
	 * @param response
	 * @return
	 * @throws SQLException
	 *
	 * @author 王湛智[wangzhanzhi@xinnet.com]
	 *
	 * @since 2014年2月13日
	 *
	 * @update:[变更日期YYYY-MM-DD][更改人姓名][变更描述]
	 */
	@Transactional(propagation=Propagation.REQUIRED,readOnly=false,rollbackFor=Exception.class)
	@RequestMapping("/allotCheck/{pageNow}")
	public String showAllotConfigCheck(@PathVariable int pageNow,HttpServletRequest request,HttpServletResponse response) throws SQLException {
		Emp currentUser = (Emp)request.getSession().getAttribute("currentUser");
		String deptId = currentUser.getId();
		String deptIdStr = deptService.getChildNodeId(deptId);
		List<Dept> deptList = deptService.getDeptListByDeptIdStr(deptIdStr);
		

		List<CustomAllotConfigDept> customAllotConfigDeptList = allotConfigCheckService.getAllotConfigCheck(deptIdStr);
		PageView pageView = new PageView();
		AgainPageView againPageView = new AgainPageView();
		if(customAllotConfigDeptList!=null){
			pageView.setPageSize(10);
			pageView.setQueryResult(customAllotConfigDeptList.size(), customAllotConfigDeptList);
		}
		if(pageNow==1){
			againPageView.againComputePageNowRecords(pageView,1,customAllotConfigDeptList);
		}else{
			againPageView.againComputePageNowRecords(pageView,pageNow,customAllotConfigDeptList);
		}
		request.setAttribute("pageView", pageView);
		request.setAttribute("deptList", deptList);
		request.setAttribute("allotCheckList", pageView.getRecords());
		return "market/check/allotconfigcheck";
	}
	/**
	 * 
	 * 功能描述：根据部门名称查询在审核状态的客户分配设置
	 *
	 * @param request
	 * @param response
	 * @param dname
	 * @return
	 * @throws SQLException
	 *
	 * @author 王湛智[wangzhanzhi@xinnet.com]
	 *
	 * @since 2014年2月13日
	 *
	 * @update:[变更日期YYYY-MM-DD][更改人姓名][变更描述]
	 */
	@Transactional(propagation=Propagation.REQUIRED,readOnly=true)
	@RequestMapping("/search")
	public String showAllotConfigCheckByDname(HttpServletRequest request,HttpServletResponse response,String dname) throws SQLException {
		PageView<CustomAllotConfigDept> pageView=new PageView<CustomAllotConfigDept>();
		Dept dept=new Dept();
		dept.setDname(dname);
		pageView=allotConfigCheckService.queryAllotConfigCheckByDname(pageView, dept);
		if(pageView!=null)
			request.setAttribute("allotCheckList", pageView.getRecords());
		return "market/check/allotconfigcheck";
	}
	/**
	 * 
	 * 功能描述：根据部门编号 批准其设置的分配规则
	 *
	 * @param request
	 * @param response
	 * @return
	 * @throws SQLException
	 *
	 * @author 王湛智[wangzhanzhi@xinnet.com]
	 * @throws IOException 
	 *
	 * @since 2014年2月13日
	 *
	 * @update:[变更日期YYYY-MM-DD][更改人姓名][变更描述]
	 */
	@Transactional(propagation=Propagation.REQUIRED,readOnly=false)
	@RequestMapping("/accept")
	public String acceptAllotConfg(HttpServletRequest request,HttpServletResponse response,String deptno) throws SQLException, IOException {
		PrintWriter writer=response.getWriter();
		allotConfigCheckService.updateAllotConfigCheckResultYes(deptno);
		writer.print(1);
		return null;
	}
	/**
	 * 
	 * 功能描述：根据部门编号 批量通过其设置的分配规则
	 *
	 * @param request
	 * @param response
	 * @return
	 * @throws SQLException
	 *
	 * @author 王湛智[wangzhanzhi@xinnet.com]
	 * @throws IOException 
	 *
	 * @since 2014年2月13日
	 *
	 * @update:[变更日期YYYY-MM-DD][更改人姓名][变更描述]
	 */
	@Transactional(propagation=Propagation.REQUIRED,readOnly=false)
	@RequestMapping("/acceptBatch")
	public String acceptBatchAllotConfg(HttpServletRequest request,HttpServletResponse response,String deptnos) throws SQLException, IOException {
		PrintWriter writer=response.getWriter();
		String[] deptno=deptnos.split(":");
		for(int i=0;i<deptno.length;i++)
			allotConfigCheckService.updateAllotConfigCheckResultYes(deptno[i]);
		writer.print(1);
		return null;
	}
	/**
	 * 
	 * 功能描述：根据部门编号 通过其设置的分配规则
	 *
	 * @param request
	 * @param response
	 * @return
	 * @throws SQLException
	 *
	 * @author 王湛智[wangzhanzhi@xinnet.com]
	 * @throws IOException 
	 *
	 * @since 2014年2月13日
	 *
	 * @update:[变更日期YYYY-MM-DD][更改人姓名][变更描述]
	 */
	@Transactional(propagation=Propagation.REQUIRED,readOnly=false)
	@RequestMapping("/refuse")
	public String refuseAllotConfg(HttpServletRequest request,HttpServletResponse response,String deptno) throws SQLException, IOException {
		PrintWriter writer=response.getWriter();
		allotConfigCheckService.updateAllotConfigCheckResultNo(deptno);
		writer.print(1);
		return null;
	}
	/**
	 * 
	 * 功能描述：根据部门编号 批量通过其设置的分配规则
	 *
	 * @param request
	 * @param response
	 * @return
	 * @throws SQLException
	 *
	 * @author 王湛智[wangzhanzhi@xinnet.com]
	 * @throws IOException 
	 *
	 * @since 2014年2月13日
	 *
	 * @update:[变更日期YYYY-MM-DD][更改人姓名][变更描述]
	 */
	@Transactional(propagation=Propagation.REQUIRED,readOnly=false)
	@RequestMapping("/refuseBatch")
	public String refuseBatchAllotConfg(HttpServletRequest request,HttpServletResponse response,String deptnos) throws SQLException, IOException {
		PrintWriter writer=response.getWriter();
		String[] deptno=deptnos.split(":");
		for(int i=0;i<deptno.length;i++)
			allotConfigCheckService.updateAllotConfigCheckResultNo(deptno[i]);
		writer.print(1);
		return null;
	}
	/**
	 * 
	 * 功能描述：显示产品规则审核
	 *
	 * @param request
	 * @param response
	 * @return
	 * @throws SQLException
	 *
	 * @author 王湛智[wangzhanzhi@xinnet.com]
	 *
	 * @since 2014年2月14日
	 *
	 * @update:[变更日期YYYY-MM-DD][更改人姓名][变更描述]
	 */
	@Transactional(propagation=Propagation.REQUIRED,readOnly=true)
	@RequestMapping("/flowCheck")
	public String showFlowCheck(HttpServletRequest request,HttpServletResponse response) throws SQLException {
		request.setAttribute("publicCycle", flowTurnConfigService.getMiddleServiceCycleInCheck());
		request.setAttribute("publicPerf", flowTurnConfigService.getPerfDayInCheck());
		request.setAttribute("publicPriority", flowTurnConfigService.getPriorityInCheck());
		request.setAttribute("productConfigList", flowTurnConfigService.getAuditProductConfig());
		return "market/check/flowconfigcheck";
	}
	/**
	 * 
	 * 功能描述：审核通过产品线设置
	 *
	 * @param request
	 * @param response
	 * @return
	 * @throws SQLException
	 *
	 * @author 王湛智[wangzhanzhi@xinnet.com]
	 * @throws IOException 
	 *
	 * @since 2014年2月14日
	 *
	 * @update:[变更日期YYYY-MM-DD][更改人姓名][变更描述]
	 */
	@Transactional(propagation=Propagation.REQUIRED,readOnly=false)
	@RequestMapping("/acceptProduct")
	public String acceptProductCheck(HttpServletRequest request,HttpServletResponse response,String ids) throws SQLException, IOException {
		String[] id=ids.split(":");
		for(int i=0;i<id.length;i++) {
			flowTurnConfigService.updateProductAuditYes(id[i]);
		}
		PrintWriter writer=response.getWriter();
		writer.print(1);
		return null;
	}
	/**
	 * 
	 * 功能描述：审核通过二次开发停留期设置
	 *
	 * @param request
	 * @param response
	 * @return
	 * @throws SQLException
	 *
	 * @author 王湛智[wangzhanzhi@xinnet.com]
	 * @throws IOException 
	 *
	 * @since 2014年2月14日
	 *
	 * @update:[变更日期YYYY-MM-DD][更改人姓名][变更描述]
	 */
	@Transactional(propagation=Propagation.REQUIRED,readOnly=false)
	@RequestMapping("/acceptCycle")
	public String updateMiddleServiceCycleAuditYes(HttpServletRequest request,HttpServletResponse response,String cycle) throws SQLException, IOException {
		int cycleValue=0;
		try {
			cycleValue=Integer.parseInt(cycle);
		} catch(Exception e) {
			log.debug(request.getRemoteAddr()+" 试图xss攻击");
			return null;
		}
		flowTurnConfigService.updateMiddleServiceCycleAuditYes(cycleValue);
		PrintWriter writer=response.getWriter();
		writer.print(1);
		return null;
	}
	/**
	 * 
	 * 功能描述：审核不通过二次开发停留期设置
	 *
	 * @param request
	 * @param response
	 * @return
	 * @throws SQLException
	 *
	 * @author 王湛智[wangzhanzhi@xinnet.com]
	 * @throws IOException 
	 *
	 * @since 2014年2月14日
	 *
	 * @update:[变更日期YYYY-MM-DD][更改人姓名][变更描述]
	 */
	@Transactional(propagation=Propagation.REQUIRED,readOnly=false)
	@RequestMapping("/refuseCycle")
	public String updateMiddleServiceCycleAuditNo(HttpServletRequest request,HttpServletResponse response) throws SQLException, IOException {
		flowTurnConfigService.updateMiddleServiceCycleAuditNo();
		PrintWriter writer=response.getWriter();
		writer.print(1);
		return null;
	}
	/**
	 * 
	 * 功能描述：审核通过业绩保留期设置
	 *
	 * @param request
	 * @param response
	 * @return
	 * @throws SQLException
	 *
	 * @author 王湛智[wangzhanzhi@xinnet.com]
	 * @throws IOException 
	 *
	 * @since 2014年2月14日
	 *
	 * @update:[变更日期YYYY-MM-DD][更改人姓名][变更描述]
	 */
	@Transactional(propagation=Propagation.REQUIRED,readOnly=false)
	@RequestMapping("/acceptPerfDay")
	public String updatePerfDayAuditYes(HttpServletRequest request,HttpServletResponse response) throws SQLException, IOException {
		flowTurnConfigService.updatePerfDayAuditYes();
		PrintWriter writer=response.getWriter();
		writer.print(1);
		return null;
	}
	/**
	 * 
	 * 功能描述：审核不通过业绩保留期设置
	 *
	 * @param request
	 * @param response
	 * @return
	 * @throws SQLException
	 *
	 * @author 王湛智[wangzhanzhi@xinnet.com]
	 * @throws IOException 
	 *
	 * @since 2014年2月14日
	 *
	 * @update:[变更日期YYYY-MM-DD][更改人姓名][变更描述]
	 */
	@Transactional(propagation=Propagation.REQUIRED,readOnly=false)
	@RequestMapping("/refusePerfDay")
	public String updatePerfDayAuditNo(HttpServletRequest request,HttpServletResponse response) throws SQLException, IOException {
		flowTurnConfigService.updatePerfDayAuditNo();
		PrintWriter writer=response.getWriter();
		writer.print(1);
		return null;
	}
	/**
	 * 
	 * 功能描述：审核不通过产品线设置
	 *
	 * @param request
	 * @param response
	 * @return
	 * @throws SQLException
	 *
	 * @author 王湛智[wangzhanzhi@xinnet.com]
	 * @throws IOException 
	 *
	 * @since 2014年2月14日
	 *
	 * @update:[变更日期YYYY-MM-DD][更改人姓名][变更描述]
	 */
	@Transactional(propagation=Propagation.REQUIRED,readOnly=false)
	@RequestMapping("/refuseProduct")
	public String refuseProductCheck(HttpServletRequest request,HttpServletResponse response,String ids) throws SQLException, IOException {
		System.out.println(ids);
		String[] id=ids.split(":");
		for(int i=0;i<id.length;i++) {
			flowTurnConfigService.updateProductAuditNo(id[i]);
		}
		PrintWriter writer=response.getWriter();
		writer.print(1);
		return null;
	}
	/**
	 * 
	 * 功能描述：修改所有产品线的二次开发停顿期 (审核通过二次开发停留期)
	 *
	 * @param request
	 * @param response
	 * @param middleServiceCycle
	 * @return
	 * @throws SQLException
	 * @throws IOException
	 *
	 * @author 王湛智[wangzhanzhi@xinnet.com]
	 *
	 * @since 2014年2月14日
	 *
	 * @update:[变更日期YYYY-MM-DD][更改人姓名][变更描述]
	 */
	@Transactional(propagation=Propagation.REQUIRED,readOnly=false)
	@RequestMapping("/setMiddleServiceCycleUse")
	public String updateMiddleServiceCycleUse(HttpServletRequest request,HttpServletResponse response,String middleServiceCycle) throws SQLException, IOException {
		flowTurnConfigService.updateMiddleServiceCycle(Integer.parseInt(middleServiceCycle));
		/**打印成功命令 前台ajax接收指令*/
		PrintWriter writer=response.getWriter();
		writer.print(1);
		return null;
	}
	/**
	 * 
	 * 功能描述：审核不通过产品优先级设置
	 *
	 * @param request
	 * @param response
	 * @return
	 * @throws SQLException
	 *
	 * @author 王湛智[wangzhanzhi@xinnet.com]
	 * @throws IOException 
	 *
	 * @since 2014年2月14日
	 *
	 * @update:[变更日期YYYY-MM-DD][更改人姓名][变更描述]
	 */
	@Transactional(propagation=Propagation.REQUIRED,readOnly=false)
	@RequestMapping("/refusePriority")
	public String updatePriorityAuditNo(HttpServletRequest request,HttpServletResponse response) throws SQLException, IOException {
		flowTurnConfigService.updatePriorityAuditNo();
		PrintWriter writer=response.getWriter();
		writer.print(1);
		return null;
	}
	/**
	 * 
	 * 功能描述：审核通过产品优先级设置
	 *
	 * @param request
	 * @param response
	 * @return
	 * @throws SQLException
	 *
	 * @author 王湛智[wangzhanzhi@xinnet.com]
	 * @throws IOException 
	 *
	 * @since 2014年2月14日
	 *
	 * @update:[变更日期YYYY-MM-DD][更改人姓名][变更描述]
	 */
	@Transactional(propagation=Propagation.REQUIRED,readOnly=false)
	@RequestMapping("/acceptPriority")
	public String updatePriorityAuditYes(HttpServletRequest request,HttpServletResponse response) throws SQLException, IOException {
		flowTurnConfigService.updatePriorityAuditYes();
		PrintWriter writer=response.getWriter();
		writer.print(1);
		return null;
	}
}
